import java.util.Iterator;
import java.util.function.Consumer;

    public class SinglyLinkedList2 implements Iterable<Integer> {
        private Node head = null;

        @Override
        public Iterator<Integer> iterator() {
            return new Iterator<Integer>() {
                Node p = head;

                @Override
                public boolean hasNext() { //是否有下一个元素
                    return p != null;
                }

                @Override
                public Integer next() {//返回当前值，并指向下一个元素
                    int value = p.value;
                    p = p.next;
                    return value;
                }
            };
        }


        private static class Node {
            int value;
            Node next;

            public Node(int value, Node next) {
                this.value = value;
                this.next = next;
            }
        }

        public void addFront(int value) {

            head = new Node(value, head);
        }

        public void loop(Consumer<Integer> consumer) {
            Node p = head;
            while (p != null) {
                consumer.accept(p.value);
                p = p.next;
            }
        }

        public Node findLast() {
            Node p = head;
            while (p != null) {
                if (p.next == null) {
                    return p;
                }
                p = p.next;
            }
            return null;
        }
        public void addLast(int value){
            Node a = findLast();
            if(a == null){
                addFront(value);
                return;
            }
            a.next = new Node(value,null);
        }
        public Node findNode(int index){
            int i = 0;
            Node p = head;
            while( p != null){
                if(i == index){
                    return p;
                }
                p = p.next;
                i++;
            }
            return null;
        }
        public int get(int index){
            Node b = findNode(index);
            if(b == null){
                throw new IllegalArgumentException(
                        String.format("index[%d] 不合法%n",index)
                );
            }
            return b.value;
        }
        public void insert(int index,int value){
            Node c = findNode(index-1);
            c.next = new Node(value,c.next);
            if( index == 0){
                addFront(value);
                return;
            }
            if ( c == null){throw new IllegalArgumentException(
                    String.format("index[%d] 不合法%n",index)
            );
            }
        }
        public void remove(int index){
            if(index == 0){
                head = head.next;
                if(head == null){
                    throw new IllegalArgumentException(
                            String.format("index[%d] 不合法%n",0)
                    );
                }
            }
            Node d = findNode(index - 1);
            if(d == null){
               throw new IllegalArgumentException(
                        String.format("index[%d] 不合法%n",index)
                );
            }
            Node removed = d.next;
            if(removed == null){
                throw new IllegalArgumentException(
                        String.format("index[%d] 不合法%n",index)
                );
            }
            d.next = removed.next;

        }
    }


